﻿using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using POS.BusinessEntities;
using POS.DataObjects;

namespace POS.DataObjectsTest
{
    [TestClass]
    public class DOEstacionTest
    {
        #region Declarations
        private static Database db;
        private const string TestUser = "testUser";
        private const int EmpresaId = 1;
        private const int SucursalId = 2;

        #endregion
        
        #region Properties: Non-Public

        private static Database Db
        {
            get
            {
                if (db == null)
                    db = DatabaseFactory.CreateDatabase();
                return db;
            }
        }

        #endregion

        #region Test Methods

        [TestMethod]
        public void GetListEstacionSinFiltro()
        {
            Db.ExecuteNonQuery(CommandType.Text, "delete estacion where empresaId = " + EmpresaId);
            CreateEstacion("TestName");
            var ds = Db.ExecuteDataSet(CommandType.Text, "select * from estacion where empresaId = " + EmpresaId + " and sucursalId = " + SucursalId + " and activo=1");
            var filtro = string.Empty;

            var results = new DOEstacion().GetList(EmpresaId, SucursalId, filtro);
            Helper.AssertGetListResult(ds, results);
        }

        [TestMethod]
        public void SaveEstacionNew()
        {
            const string name = "TestName";
            const int consecutivoDevolucion = 0;
            const int consecutivoFactura = 1;
            const string impresoraFactura = "ImpFact";
            const string impresoraDevolucion = "ImpDev";
            const string impresoraTiquete = "ImpTiq";
            
            DeleteEstacion(name);
            var entity = new BEEstacion 
                        { Nombre = name, 
                          ConsecutivoDevolucion = consecutivoDevolucion, 
                          ConsecutivoFactura = consecutivoFactura, 
                          ImpresoraDevoluciones = impresoraDevolucion, 
                          ImpresoraFacturas = impresoraFactura, 
                          ImpresoraTiquetes = impresoraTiquete };
            new DOEstacion().Save(entity, EmpresaId, SucursalId, TestUser);

            var ds = Db.ExecuteDataSet(CommandType.Text, "select * from estacion where empresaId=" + EmpresaId);
            Assert.IsNotNull(ds);
            Assert.AreEqual(1, ds.Tables.Count);
            Assert.AreEqual(1, ds.Tables[0].Rows.Count);
            var row = ds.Tables[0].Rows[0];
            Assert.IsTrue((int)row["EstacionId"] > 0);
            Assert.AreEqual(name, row["Nombre"]);
            Assert.AreEqual(consecutivoDevolucion, row["consecutivoDevolucion"]);
            Assert.AreEqual(consecutivoFactura, row["consecutivoFactura"]);
            Assert.AreEqual(impresoraDevolucion, row["impresoraDevoluciones"]);
            Assert.AreEqual(impresoraFactura, row["impresoraFacturas"]);
            Assert.AreEqual(impresoraTiquete, row["impresoraTiquetes"]);
            Assert.AreEqual(true, row["Activo"]);
            Assert.AreEqual(TestUser, row["ModificadoPor"]);
            Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaModificacion"].ToString()).ToShortDateString());
            Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaCreacion"].ToString()).ToShortDateString());
        }

        [TestMethod]
        public void SaveEstacionExisting()
        {
            const string name = "TestName";
            const int consecutivoDevolucion = 0;
            const int consecutivoFactura = 1;
            const string impresoraFactura = "ImpFact";
            const string impresoraDevolucion = "ImpDev";
            const string impresoraTiquete = "ImpTiq";

            DeleteEstacion(name);
            var id = CreateEstacion(name);
            var expectedName = name + DateTime.Now.Ticks;
            var expectedUser = TestUser + DateTime.Now.Ticks;
            var entity = new BEEstacion
            {
                Id = id,
                Nombre = expectedName,
                ConsecutivoDevolucion = consecutivoDevolucion,
                ConsecutivoFactura = consecutivoFactura,
                ImpresoraDevoluciones = impresoraDevolucion,
                ImpresoraFacturas = impresoraFactura,
                ImpresoraTiquetes = impresoraTiquete
            };

            new DOEstacion().Save(entity, EmpresaId, SucursalId, expectedUser);

            var ds = Db.ExecuteDataSet(CommandType.Text, "select * from estacion where empresaId=" + EmpresaId);
            Assert.IsNotNull(ds);
            Assert.AreEqual(1, ds.Tables.Count);
            Assert.AreEqual(1, ds.Tables[0].Rows.Count);
            var row = ds.Tables[0].Rows[0];
            Assert.IsTrue((int)row["EstacionId"] > 0);
            Assert.AreEqual(expectedName, row["Nombre"]);
            Assert.AreEqual(consecutivoDevolucion, row["consecutivoDevolucion"]);
            Assert.AreEqual(consecutivoFactura, row["consecutivoFactura"]);
            Assert.AreEqual(impresoraDevolucion, row["impresoraDevoluciones"]);
            Assert.AreEqual(impresoraFactura, row["impresoraFacturas"]);
            Assert.AreEqual(impresoraTiquete, row["impresoraTiquetes"]);
            Assert.AreEqual(true, row["Activo"]);
            Assert.AreEqual(expectedUser, row["ModificadoPor"]);
            Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaModificacion"].ToString()).ToShortDateString());
            Assert.AreEqual(DateTime.Today.ToShortDateString(), DateTime.Parse(row["FechaCreacion"].ToString()).ToShortDateString());
        }


        #endregion

        #region Test Utilities

        private static void DeleteEstacion(string name)
        {
            Db.ExecuteNonQuery(CommandType.Text, "delete estacion where empresaId = " + EmpresaId + " and nombre='" + name + "'");
        }

        private static int CreateEstacion(string name)
        {
            const int id = 999;
            var sql = "Insert into Estacion " +
                      " (EstacionId, EmpresaId, SucursalId, Activo, Nombre, ConsecutivoFactura, ConsecutivoDevolucion, " +
                      "  ImpresoraFacturas, ImpresoraDevoluciones, ImpresoraTiquetes, FechaCreacion, FechaModificacion, ModificadoPor) " +
                      "Values (" + id + ", " + EmpresaId + "," + SucursalId + ", 1, '" + name + "',0, 1, 'ImpFact', 'ImpDev', 'ImpTiq', getdate(), getdate(), 'TestUser')";
            Db.ExecuteNonQuery(CommandType.Text, sql);
            return id;
        }
        
        #endregion
    }
}
